require 'ostruct'

class LoginController < ApplicationController
	before_filter :redirect,
	  :only => [ :index, :google, :openid]

	def index
		
	end

	def google
		@openIdUrl = url_for :controller => :login, :action => :google
    auth ProviderUtils::GOOGLE_AUTH_ID,{ :required => [:nickname, :email] }, :GOOGLE
	end

	def openid
    @openIdUrl = url_for :controller => :login, :action => :openid
		if not params[:sent_url].nil?
			if params[:openid_url].empty?
				flash[:error] = I18n.t :tr_openid_not_supplied
			else
				auth params[:openid_url], { :required => [:nickname, :email, :fullname, :language] }, :OPEN_ID
			end
    elsif params[:open_id_complete].eql?("1")
				auth params[:openid_url], { :required => [:nickname, :email, :fullname, :language] }, :OPEN_ID
    end
	end  
	
	protected
		def root_url
			@openIdUrl #url_for :controller => :login, :action => :google #"http://localhost:3000/login/google" #"http://localhost:3000/login"#openid_url
		end
	
	private

		def redirect
			redirect_to :controller => "canelada" if session[:player]
		end
		
		def regGoogle _registrationData
			@user.email = _registrationData.data["http://schema.openid.net/contact/email"][0]
			@user.firstname = _registrationData.data["http://axschema.org/namePerson/first"][0]
			@user.lastname = _registrationData.data["http://axschema.org/namePerson/last"][0]
      @user.language = @user.language = Language.find_by_code(_registrationData.data["http://axschema.org/pref/language"][0])
			@user.nickname = @user.firstname
      @user.fullname = @user.firstname + " " + @user.lastname
		end
		
		def regOpenId _registrationData
      @user.email = _registrationData.data["email"]
			@user.nickname = _registrationData.data["nickname"]
      @user.fullname = _registrationData.data["fullname"]
      @user.language = Language.find_by_code(_registrationData.data["language"])
		end
	
#    def auth _openIdUrl, _required, _type
#      @user = OpenStruct.new
#      @user.identity_url = "localhost"
#
#			@user.email = "hdvianna@gmail.com"
#			@user.firstname = "Henrique"
#			@user.lastname = "Vianna"
#      @user.language = "pt-BR"
#			@user.nickname = "hdvianna"
#      @user.fullname = "Henrique Vianna"
#
#      conditions = ProviderUtils.find_player_condition(_type, {:email => @user.email, :auth_id => @user.identity_url})
#      @player = Player.first(conditions)
#      session[:user_id] = @user
#      session[:player] = @player
#      if session[:player].nil?
#        redirect_to(:controller => "sign_up", :action => "index")
#      else
#        jumpto = session[:jumpto] || { :controller => "canelada" }
#        session[:jumpto] = nil
#        redirect_to(jumpto)
#      end
#  end

		def auth _openIdUrl, _required, _type
			#params[:openid_url], :required => [:nickname, :email]
      #http://drhanschucrutz.myopenid.com
      #http://openid.net/specs/openid-simple-registration-extension-1_0.htm
			authenticate_with_open_id(_openIdUrl, _required) do |result, identity_url, registration|
				if result.successful?
					@user = OpenStruct.new
					@user.identity_url = identity_url
          @user.auth_provider = _type
					case _type
					when :GOOGLE
						regGoogle registration
					else
						regOpenId registration
					end
          conditions = ProviderUtils.find_player_condition(_type, {:email => @user.email, :auth_id => @user.auth_provider})
          @player = Player.first(conditions)
          session[:user_id] = @user
          session[:player] = @player
          if session[:player].nil?
            redirect_to(:controller => "sign_up")
          else
            jumpto = session[:jumpto] || { :controller => "canelada" }
            session[:jumpto] = nil
            redirect_to(jumpto)
          end
				else
					flash[:error] = result.message
					redirect_to @openIdUrl
				end
			end
		end

end
